﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DTgame_CameraRayChk : MonoBehaviour
{
    float timer = 0;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //每0.2秒获取一次当前用户输入
        if (timer == 0)
        {
            //鼠标左键按下
            if (Input.GetMouseButton(0))
            {
                //print("tick");
                RaycastHit[] info = rayChk();
                //print(info.collider.gameObject.name+" distance:" + info.distance);
                foreach (var item in info)
                {
                    item.collider.SendMessage("BuillTuret", SendMessageOptions.DontRequireReceiver);
                }
                //info.collider.SendMessageUpwards("BuillTuret", SendMessageOptions.DontRequireReceiver);
            }

            //鼠标右键按下
            if (Input.GetMouseButton(1))
            {
                RaycastHit[] info = rayChk();
                foreach (var item in info)
                {
                    item.collider.SendMessage("ClearTuret", SendMessageOptions.DontRequireReceiver);
                }
                //info.collider.SendMessageUpwards("ClearTuret", SendMessageOptions.DontRequireReceiver);
            }
            timer += Time.deltaTime;
        }
        else
        {
            timer += Time.deltaTime;
            if (timer > 0.2f)
            {
                timer = 0;
            }
        }
    }

    private RaycastHit[] rayChk()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit[] info;
        info = Physics.RaycastAll(ray);
        return info;
    }
}
